package com.example.homework07.Room;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

@Database(entities = {User.class}, version = 1)
public abstract class MyRoomDatabase extends RoomDatabase {
    private static MyRoomDatabase instance;

    public abstract UserDao userDao();

    static MyRoomDatabase getInstance(Context context) {
        if (instance == null) {
            instance = Room.databaseBuilder(context.getApplicationContext(), MyRoomDatabase.class, "room").allowMainThreadQueries().build();
            //这里为了测试，允许在主线程中使用Room库
            //但正式的应用不要这样用，数据库操作时会卡UI界面
            //建议将数据库操作改成异步的！
        }
        return instance;
    }
}
